package com.pierfrancescosoffritti.androidyoutubeplayer.core.sampleapp.examples.customUiExample;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.PlayerConstants;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.utils.YouTubePlayerTracker;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.aytplayersample.R;

class CustomPlayerUiController extends AbstractYouTubePlayerListener {
  private final YouTubePlayerTracker playerTracker;
  private final Context context;
  private final YouTubePlayer youTubePlayer;
  private final YouTubePlayerView youTubePlayerView;
  // panel is used to intercept clicks on the WebView, I don't want the user to be able to click the WebView directly.
  private View panel;
  private View progressbar;
  private TextView videoCurrentTimeTextView;
  private TextView videoDurationTextView;
  private boolean fullscreen = false;

  CustomPlayerUiController(Context context, View customPlayerUi, YouTubePlayer youTubePlayer, YouTubePlayerView youTubePlayerView) {
    this.context = context;
    this.youTubePlayer = youTubePlayer;
    this.youTubePlayerView = youTubePlayerView;

    playerTracker = new YouTubePlayerTracker();
    youTubePlayer.addListener(playerTracker);

    initViews(customPlayerUi);
  }

  private void initViews(View playerUi) {
    panel = playerUi.findViewById(R.id.panel);
    progressbar = playerUi.findViewById(R.id.progressbar);
    videoCurrentTimeTextView = playerUi.findViewById(R.id.video_current_time);
    videoDurationTextView = playerUi.findViewById(R.id.video_duration);
    Button playPauseButton = playerUi.findViewById(R.id.play_pause_button);
    Button enterExitFullscreenButton = playerUi.findViewById(R.id.enter_exit_fullscreen_button);

    playPauseButton.setOnClickListener((view) -> {
      if (playerTracker.getState() == PlayerConstants.PlayerState.PLAYING) youTubePlayer.pause();
      else youTubePlayer.play();
    });

    enterExitFullscreenButton.setOnClickListener((view) -> {
      if (fullscreen) youTubePlayerView.wrapContent();
      else youTubePlayerView.matchParent();

      fullscreen = !fullscreen;
    });
  }

  @Override
  public void onReady(@NonNull YouTubePlayer youTubePlayer) {
    progressbar.setVisibility(View.GONE);
  }

  @Override
  public void onStateChange(@NonNull YouTubePlayer youTubePlayer, @NonNull PlayerConstants.PlayerState state) {
    if (state == PlayerConstants.PlayerState.PLAYING || state == PlayerConstants.PlayerState.PAUSED || state == PlayerConstants.PlayerState.VIDEO_CUED)
      panel.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
    else if (state == PlayerConstants.PlayerState.BUFFERING)
      panel.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
  }

  @SuppressLint("SetTextI18n")
  @Override
  public void onCurrentSecond(@NonNull YouTubePlayer youTubePlayer, float second) {
    videoCurrentTimeTextView.setText(second + "");
  }

  @SuppressLint("SetTextI18n")
  @Override
  public void onVideoDuration(@NonNull YouTubePlayer youTubePlayer, float duration) {
    videoDurationTextView.setText(duration + "");
  }
}
